package leetcode;

public class LC29 {
    public static void main(String[] args) {
        LC29 lc29 = new LC29();
        ListNode.print(lc29.removeNthFromEnd(ListNode.ln(new int[]{1,2,3,4,5}),2));
        ListNode.print(lc29.removeNthFromEnd(ListNode.ln(new int[]{1}),1));
        ListNode.print(lc29.removeNthFromEnd(ListNode.ln(new int[]{1,2}),1));
    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int size = 0;
        ListNode temp = head;
        while(temp != null){
            size++;
            temp = temp.next;
        }
        temp = head;
        for (int i = 0; i < size - n - 1; i++) {
            temp = temp.next;
        }
        System.out.println(temp.val);
        if (size == n) head = head.next;
        else temp.next = temp.next.next;
        return head;
    }
}
